package cn.edu.buu.hospital.service.impl;

import cn.edu.buu.hospital.entity.Appointment;
import cn.edu.buu.hospital.entity.Pay;
import cn.edu.buu.hospital.mapper.AppointmentMapper;
import cn.edu.buu.hospital.mapper.VisitMapper;
import cn.edu.buu.hospital.service.AppointmentService;
import cn.edu.buu.hospital.service.PayService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
@Service
@Transactional
public class AppointmentServiceImpl implements AppointmentService {

    @Autowired
    private AppointmentMapper appointmentMapper;
    
    @Autowired
    private VisitMapper visitMapper;
    
    @Autowired
    private PayService payService;

    @Override
    public void add(Appointment appointment) {
        appointmentMapper.insert(appointment);
    }

    @Override
    public void removeById(Long appointmentId) {
        appointmentMapper.deleteById(appointmentId);
    }

    @Override
    public void modify(Appointment appointment) {
        appointmentMapper.update(appointment);
    }

    @Override
    public Appointment findById(Long appointmentId) {
        return appointmentMapper.selectById(appointmentId);
    }

    @Override
    public List<Appointment> findAll() {
        return appointmentMapper.selectAll();
    }

    @Override
    public PageInfo<Appointment> find(int pageNum, int pageSize, Appointment condition) {
        PageHelper.startPage(pageNum, pageSize);
        List<Appointment> appointments = appointmentMapper.selectByCondition(condition, null);
        return new PageInfo<>(appointments);
    }
    
    @Override
    public PageInfo<Appointment> findByUserId(int pageNum, int pageSize, Long userId, Appointment condition) {
        PageHelper.startPage(pageNum, pageSize);
        List<Appointment> appointmentList = appointmentMapper.selectByUserId(userId);
        return new PageInfo<>(appointmentList);
    }

    @Override
    public List<Appointment> findByVisitId(Long visitId) {
        return appointmentMapper.selectByVisitId(visitId);
    }

    // 新增方法：确认预约
    @Override
    public void confirmAppointment(Long appointmentId) {
        // 更新预约状态为已完成(1)
        appointmentMapper.updateStatus(appointmentId, 1);
        
        // 创建对应的支付记录
        Pay pay = new Pay();
        pay.setAppointmentid(appointmentId);
        // 设置默认金额（实际应根据业务规则确定）
        pay.setAmount(BigDecimal.valueOf(100.00));
        // 设置支付状态为未支付(0)
        pay.setPaystatus(0);
        // 设置创建时间为当前时间
        pay.setCreatetime(LocalDateTime.now());
        
        // 保存支付记录
        payService.add(pay);
    }

    // 新增方法：取消预约
    @Override
    public void cancelAppointment(Long appointmentId) {
        // 先查询该预约对应的排班ID
        Appointment appointment = appointmentMapper.selectById(appointmentId);
        if (appointment != null && appointment.getVisitid() != null) {
            // 更新预约状态为已取消(2)
            appointmentMapper.updateStatus(appointmentId, 2);
            // 减少对应排班的已预约数量
            visitMapper.decreaseUsedNum(appointment.getVisitid());
        }
    }

    @Override
    public long countTodayAppointments() {
        return appointmentMapper.countTodayAppointments();
    }
    
    @Override
    public Appointment findByNumber(String appointmentNo) {
        Appointment condition = new Appointment();
        condition.setNumber(appointmentNo);
        List<Appointment> appointments = appointmentMapper.selectByCondition(condition, null);
        if (appointments != null && !appointments.isEmpty()) {
            return appointments.get(0);
        }
        return null;
    }
    
    @Override
    public List<Appointment> findByUserId(Long userId) {
        return appointmentMapper.selectByUserId(userId);
    }
}